package org.falak.receivers;

import org.falak.logic.FindPhoneNosInfo;
import org.falak.ui.ShowInfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class OutgoingCallReceiver extends BroadcastReceiver {

	private FindPhoneNosInfo logic;
	private ShowInfo info;
	private Context context;

	@Override
	public void onReceive(Context contextGot, Intent intent) {
		Bundle bundle = intent.getExtras();
		this.context = contextGot;

		if (null == bundle)
			return;

		String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

		Log.i("OutgoingCallReceiver", phonenumber);
		Log.i("OutgoingCallReceiver", bundle.toString());

		// Skip for small numbers
		if (phonenumber.length() > 4) {
			this.getInfo().showInfo(context, getLocation(phonenumber));
		}
	}

	private String getLocation(String phoneNumber) {
		// TODO Auto-generated method stub
		String info = null;
		if (this.getLogic().isIndian(phoneNumber)) {
			info = this.getLogic().getIndianInfo(phoneNumber);
		} else {
			info = this.getLogic().getIntlInfo(phoneNumber);
		}
		return info;
	}

	public FindPhoneNosInfo getLogic() {
		if (logic == null) {
			logic = new FindPhoneNosInfo(this.context);
		}
		return logic;
	}

	public ShowInfo getInfo() {
		if (info == null) {
			info = new ShowInfo();
		}
		return info;
	}

}
